<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class IndexChannel extends \app\ApiRest
{
    protected $model;
    protected $channel_info;
    protected $order_model;
    protected $user_model;
    protected $channel_name;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\ChannelList();
        $this->order_model = new \app\massage\model\Order();
        $this->user_model = new \app\massage\model\User();
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [2, 3]];
        $this->channel_info = $this->model->dataInfo($cap_dis);
        if(empty($this->channel_name)) {
            $this->channel_name = getConfigSetting($this->_uniacid, "channel_menu_name");
        }
        if(empty($this->channel_info)) {
            $this->errorMsg("你还不是" . $this->channel_name);
        }
    }
    public function index()
    {
        $this->order_model->coachBalanceArr($this->_uniacid);
        $input = $this->_param;
        $data = $this->channel_info;
        $order_data = $this->order_model->channelData($this->channel_info["id"], $input);
        $data = array_merge($order_data, $data);
        $data["total_cash"] = $data["all_cash"];
        $data["tax_point"] = getConfigSetting($this->_uniacid, "tax_point");
        if(!empty($data["salesman_id"])) {
            $salesman_model = new \app\massage\model\Salesman();
            $data["salesman_name"] = $salesman_model->where(["id" => $data["salesman_id"]])->where("status", "in", [2])->value("user_name");
        }
        if(!empty($data["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $data["agent_name"] = $admin_model->where(["id" => $data["admin_id"], "status" => 1])->value("agent_name");
        }
        $data["balance"] = 0 <= $data["balance"] ? $data["balance"] : getConfigSetting($this->_uniacid, "channel_balance");
        return $this->success($data);
    }
    public function channelQr()
    {
        $input = $this->_param;
        $type = getConfigSetting($this->_uniacid, "wechat_qr_type");
        $key = "channel_qrssssss" . $this->channel_info["id"] . "-" . $this->is_app . "-" . $type;
        $qr = getCache($key, 99999999);
        if(empty($qr)) {
            if($this->is_app == 0) {
                $input["page"] = "user/pages/gzh";
                $input["channel_id"] = $this->channel_info["id"];
                $qr = $this->user_model->orderQr($input, $this->_uniacid);
            } elseif($type == 0) {
                $page = "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/user/pages/gzh?channel_id=" . $this->channel_info["id"];
                $qr = base64ToPng(getCode($this->_uniacid, $page));
            } else {
                $core = new \longbingcore\wxcore\WxSetting($this->_uniacid);
                $qr = $core->qrCode($this->channel_info["id"] . "_channel");
            }
            setCache($key, $qr, 8640000, 99999999);
        }
        $qr = !empty($qr) ? $qr : "https://" . $_SERVER["HTTP_HOST"] . "/favicon.ico";
        return $this->success($qr);
    }
    public function channelQrList()
    {
        $qr_model = new \app\massage\model\ChannelQr();
        $dis = ["status" => 1, "channel_id" => $this->channel_info["id"]];
        $data = $qr_model->dataList($dis, 10);
        return $this->success($data);
    }
    public function channelQrSelect()
    {
        $qr_model = new \app\massage\model\ChannelQr();
        $dis = ["status" => 1, "channel_id" => $this->channel_info["id"]];
        $data = $qr_model->where($dis)->field("id,title")->order("id desc")->select()->toArray();
        return $this->success($data);
    }
    public function orderList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.pay_type", ">", 1];
        $dis[] = ["a.channel_id", "=", $this->channel_info["id"]];
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if(isset($input["qr_id"])) {
            $dis[] = ["a.channel_qr_id", "=", $input["qr_id"]];
        }
        $where = [];
        if(!empty($input["name"])) {
            $where[] = ["b.goods_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.order_code", "like", "%" . $input["name"] . "%"];
        }
        $data = $this->order_model->channelDataList($dis, $where);
        if(!empty($data["data"])) {
            $refund_model = new \app\massage\model\RefundOrder();
            $share_model = new \app\massage\model\CommShare();
            foreach ($data["data"] as &$v) {
                $v["true_service_price"] += $v["material_price"];
                $v["true_service_price"] = round($v["true_service_price"], 2);
                $v["refund_price"] = $refund_model->where(["order_id" => $v["id"], "status" => 2])->sum("service_price");
                $v["point_cash"] = $share_model->where(["comm_id" => $v["comm_id"], "cash_type" => 1])->sum("share_cash");
            }
        }
        return $this->success($data);
    }
    public function applyWallet()
    {
        $input = $this->_input;
        if(empty($input["apply_price"]) || $input["apply_price"] < 0) {
            $this->errorMsg("提现费最低一分");
        }
        if($this->channel_info["cash"] < $input["apply_price"]) {
            $this->errorMsg("余额不足");
        }
        $tax_point = getConfigSetting($this->_uniacid, "tax_point");
        $balance = 100 - $tax_point;
        $key = "channel_wallet" . $this->getUserId();
        incCache($key, 1, $this->_uniacid);
        $value = getCache($key, $this->_uniacid);
        if($value != 1) {
            decCache($key, 1, $this->_uniacid);
            $this->errorMsg("网络错误，请刷新重试");
        }
        \think\facade\Db::startTrans();
        $res = $this->model->dataUpdate(["id" => $this->channel_info["id"]], ["cash" => $this->channel_info["cash"] - $input["apply_price"]]);
        if($res != 1) {
            \think\facade\Db::rollback();
            decCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => $this->channel_info["id"], "admin_id" => $this->channel_info["admin_id"], "total_price" => $input["apply_price"], "balance" => $balance, "apply_price" => round($input["apply_price"] * $balance / 100, 2), "service_price" => round($input["apply_price"] * $tax_point / 100, 2), "code" => orderCode(), "tax_point" => $tax_point, "text" => $input["text"], "type" => 5, "last_login_type" => $this->is_app, "apply_transfer" => !empty($input["apply_transfer"]) ? $input["apply_transfer"] : 0];
        $wallet_model = new \app\massage\model\Wallet();
        $res = $wallet_model->dataAdd($insert);
        if($res != 1) {
            \think\facade\Db::rollback();
            decCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        \think\facade\Db::commit();
        decCache($key, 1, $this->_uniacid);
        return $this->success($res);
    }
    public function walletList()
    {
        $wallet_model = new \app\massage\model\Wallet();
        $input = $this->_param;
        $dis = ["coach_id" => $this->channel_info["id"]];
        if(!empty($input["status"])) {
            $dis["status"] = $input["status"];
        }
        $dis["type"] = 5;
        $data = $wallet_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
            }
        }
        $data["extract_total_price"] = $wallet_model->capCash($this->channel_info["id"], 2, 5);
        $data["personal_income_tax_text"] = getConfigSetting($this->_uniacid, "personal_income_tax_text");
        return $this->success($data);
    }
    public function channelQrInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $qr_model = new \app\massage\model\ChannelQr();
        $data = $qr_model->dataInfo($dis);
        if(empty($data["qr_img"])) {
            $img = $qr_model->channelQrImg($data);
            $qr_model->dataUpdate($dis, ["qr_img" => $img]);
            $data = $qr_model->dataInfo($dis);
        }
        return $this->success($data);
    }
}

?>